package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StatServicelmpl implements StatService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    /**
     * 功能:用户统计页面
     * 作者:whj
     */
    @Override
    public UserStatisticsVo userStat() {
        UserStatisticsVo userStatisticsVo = new UserStatisticsVo();
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("users");
        userStatisticsVo.setColumns(columns);
        UserExample userExample = new UserExample();
        //时间排序
        userExample.setOrderByClause("add_time");
        List<User> users = userMapper.selectByExample(userExample);
        List<UserStaRowsVo> rows = new ArrayList<>();
        //取值,遍历,计数,删除,循环
        int count = 0;
        for (int i = 0; i < users.size(); ) {
            String sub1 = users.get(i).getAddTime().toString().substring(4, 10);
            String sub2 = users.get(i).getAddTime().toString().substring(24, 28);
            for (User user : users) {
                if (user.getAddTime().toString().contains(sub1) && user.getAddTime().toString().contains(sub2)) {
                    count++;
                }
            }
            UserStaRowsVo userStaRowsVo = new UserStaRowsVo();
            Date day = users.get(i).getAddTime();
            for (int j = users.size() - 1; j >= 0; j--) {
                if (users.get(j).getAddTime().toString().contains(sub1) && users.get(j).getAddTime().toString().contains(sub2)) {
                    users.remove(j);
                }
            }
            userStaRowsVo.setDay(day);
            userStaRowsVo.setUsers(count);
            rows.add(userStaRowsVo);
            count = 0;
        }
        userStatisticsVo.setRows(rows);
        return userStatisticsVo;
    }

    /**
     * 功能:订单统计页面
     * 作者:whj
     */
    @Override
    public OrderStatisticsVo orderStat() {
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");
        orderStatisticsVo.setColumns(columns);
        OrderExample orderExample = new OrderExample();
        //时间排序
        orderExample.setOrderByClause("add_time");
        List<Order> orders = orderMapper.selectByExample(orderExample);
        List<OrderStaRowsVo> rows = new ArrayList<>();
        //取值,遍历,计数,删除,循环
        int orderCount = 0;
        int userCount;
        double amount = 0;
        double pcr = 0;
        for (int i = 0; i < orders.size(); ) {
            String addtime = orders.get(i).getAddTime().toString();
            Set<Integer> userSet = new HashSet<>();
            for (Order order : orders) {
                if (order.getAddTime().toString().contains(addtime)) {
                    orderCount++;
                    userSet.add(order.getUserId());
                    amount = amount + order.getActualPrice().longValue();
                }
            }
            userCount = userSet.size();
            pcr = amount / userCount;
            OrderStaRowsVo orderStaRowsVo = new OrderStaRowsVo();
            Date day = orders.get(i).getAddTime();
            for (int j = orders.size() - 1; j >= 0; j--) {
                if (orders.get(j).getAddTime().toString().contains(addtime)) {
                    orders.remove(j);
                }
            }
            orderStaRowsVo.setDay(day);
            orderStaRowsVo.setOrders(orderCount);
            orderStaRowsVo.setCustomers(userCount);
            orderStaRowsVo.setAmount(amount);
            orderStaRowsVo.setPcr(pcr);
            rows.add(orderStaRowsVo);
            userSet = null;
            orderCount = 0;
            amount = 0;
            pcr = 0;
        }
        orderStatisticsVo.setRows(rows);
        return orderStatisticsVo;
    }

    /**
     * 功能:商品统计页面
     * 作者:whj
     */
    @Override
    public GoodsStatisticsVo goodsStat() {
        GoodsStatisticsVo goodsStatisticsVo = new GoodsStatisticsVo();
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");
        goodsStatisticsVo.setColumns(columns);
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        //时间排序
        orderGoodsExample.setOrderByClause("add_time");
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        List<GoodsStaRowsVo> rows = new ArrayList<>();
        //取值,遍历,计数,删除,循环
        int orderCount = 0;
        int goodsCount = 0;
        double amount = 0;
        for (int i = 0; i < orderGoods.size(); ) {
            String addtime = orderGoods.get(i).getAddTime().toString();
            for (OrderGoods goods : orderGoods) {
                if (goods.getAddTime().toString().contains(addtime)) {
                    orderCount++;
                    goodsCount = goodsCount + goods.getNumber();
                    amount = goods.getPrice().longValue() * goodsCount;
                }
            }
            GoodsStaRowsVo goodsStaRowsVo = new GoodsStaRowsVo();
            Date day = orderGoods.get(i).getAddTime();
            for (int j = orderGoods.size() - 1; j >= 0; j--) {
                if (orderGoods.get(j).getAddTime().toString().contains(addtime)) {
                    orderGoods.remove(j);
                }
            }
            goodsStaRowsVo.setDay(day);
            goodsStaRowsVo.setOrders(orderCount);
            goodsStaRowsVo.setProducts(goodsCount);
            goodsStaRowsVo.setAmount(amount);
            rows.add(goodsStaRowsVo);
            goodsCount = 0;
            orderCount = 0;
            amount = 0;
        }
        goodsStatisticsVo.setRows(rows);
        return goodsStatisticsVo;
    }
}
